package com.why.serviceedu.controller;


import com.why.commonresults.Result;
import com.why.serviceedu.bean.EduChapter;
import com.why.serviceedu.bean.chapter.ChapterVo;
import com.why.serviceedu.service.EduChapterService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 课程 前端控制器
 * </p>
 *
 * @author why
 * @since 2021-02-01
 */
@RestController
@RequestMapping("/eduservice/edu-chapter")
@CrossOrigin
public class EduChapterController {
    @Autowired
    private EduChapterService service;

    /**
     * 课程大纲列表，根据课程的id进行查询
     * @param courseId
     * @return
     */
    @GetMapping("/getAllChapter/{courseId}")
    public Result getAllChapter(@PathVariable String courseId){

      List<ChapterVo> list= service.getAllChapterVideoByCourseId(courseId);

        return  Result.ok().data("list",list);
    }

    /**
     * 添加章节
     * @param chapter
     * @return
     */
    @PostMapping("/addChapter")
    public Result addChapter(@RequestBody EduChapter chapter){
        service.save(chapter);
        return Result.ok();
    }


    /**
     * 根据id得到章节
     * @param chapterId
     * @return
     */
    @GetMapping("/getChapter/{chapterId}")
    public Result getChapter(@PathVariable String chapterId){
        EduChapter chapter = service.getById(chapterId);
        return Result.ok().data("chapter",chapter);
    }

    /**
     * 修改章节
     * @param chapter
     * @return
     */
    @PostMapping("/updateChapter")
    public Result updateChapterById(@RequestBody EduChapter chapter){
        service.updateById(chapter);
        return Result.ok();
    }

    /**
     * 删除章节
     *
     */
    @GetMapping("/deleteChapter/{chapterId}")
    public Result deleteChapterById(@PathVariable String chapterId){
        boolean flag = service.deleteById(chapterId);
        return flag?Result.ok():Result.error();
    }
}

